{template '_header'}
<div class="page-heading"> 
    <h2>生成结算单</h2>
</div>

{if empty($set['paytype'])}

<div class="alert alert-danger">
    未设置分红发放周期，立即去<a href="{php echo webUrl('fullownership/set',array('tab'=>'money'))}">【设置】</a>
</div>
{else}

<form action="" method="post" class="form-horizontal form-validate" enctype="multipart/form-data">

    <div class="form-group">
        <label class="col-sm-2 control-label">上次结算</label>
        <div class="col-sm-9 col-xs-12">
            <div class="form-control-static">
                {if empty($bill)}
                未发放
                {else}
                时间: {php echo date('Y-m-d',$bill['starttime'])} - {php echo date('Y-m-d',$bill['endtime'])}<br/><br/>
                类型: {if $bill['paytype']==1}
                <label class="label label-success">按月</label>
                {elseif $bill['paytype']==2}
                <label class="label label-warning">按周</label>
                {/if}

                <br/><br/>
                状态: {if $bill['status']==1}<br/>
                <label class="label label-success">已发放</label>
                {else}
                <label class="label label-default">未发放</label>
                {/if}
                {/if}
            </div>
        </div>
    </div>

    <div class="form-group">
        <label class="col-sm-2 control-label">{if $set['paytype']==2}按周{else}按月{/if}发放</label>
        <div class="col-sm-9 col-xs-12">
            <select name="year" class='form-control input-sm select-sm' style="float:left;">
                {loop $years $y}
                <option value="{$y}" {if $y==date('Y')}selected="selected"{/if}>{$y}年</option>
                {/loop}
            </select>
            <select name="month" class='form-control input-sm select-sm' style="float:left;">
                <option value=''>月份</option>
                {loop $months $m}
                <option value="{$m}" {if $m==date('m')+1}selected="selected"{/if}>{$m}月</option>
                {/loop}
            </select>
            {if $set['paytype']==2}

            <select name="week" class='form-control input-sm select-sm' style="float:left;">
                <option value="1" {if $week==1}selected="selected"{/if}>第1周</option>
                <option value="2" {if $week==2}selected="selected"{/if}>第2周</option>
                <option value="3" {if $week==3}selected="selected"{/if}>第3周</option>
                <option value="4" {if $week==4}selected="selected"{/if}>第4周</option>
            </select>

            {/if}

        </div>
    </div>


    <div id="datas">

    </div>

</form>
<script>
    function loadDetail() {
        var year = $('select[name=year]').val();
        var month = $('select[name=month]').val();
        var week = $('select[name=week]').val();

        $('select[name=year],select[name=month],select[name=week],#btn').attr('disabled', true);
        $('#btn').val('正在分析数据...').unbind('click');


$('#datas').html('正在加载...');
        $.ajax({
            url: "{php echo webUrl('fullownership/bonus/loaddetail')}",
            dataType: 'html',
            data: {
                year: year, month: month, week: week
            },
            success: function (ret) {

                var result = ret.result;

                $('select[name=year],select[name=month],select[name=week],#btn').removeAttr('disabled');
                $('#datas').html(ret);
                $('#btn').val('生成分红结算单').bind('click', function () {
                     createBill();
                });
            }
        });
    }
    function createBill() {

        var year = $('select[name=year]').val();
        var month = $('select[name=month]').val();
        var week = $('select[name=week]').val();
        if (!$('#bonusmoney1').isNumber()) {
             tip.msgbox.err('请输入数字最终分红!');
            return;
        }
        var money = parseFloat($('#bonusmoney1').val());
        if (money < 0.01) {
            tip.msgbox.err('分红至少大于1分钱!');
            return;
        }
        tip.confirm('确认要生成分红结算单？', function () {

            $('select[name=year],select[name=month],select[name=week],#btn').attr('disabled', true);
            $('#btn').val('正在生成结算单...');

            $.ajax({
                url: "{php echo webUrl('fullownership/bonus/build')}",
                type: 'post',
                dataType: 'json',
                data: {
                    year: year, month: month, week: week,bonusmoney: $('#bonusmoney1').val()
                },
                success: function (ret) {

                    var result = ret.result;
                    if (ret.status != 1) {
                        $('select[name=year],select[name=month],select[name=week],#btn').removeAttr('disabled');
                        tip.msgbox.err(result.message);
                        return;
                    }
                    if (result.old) {
                        $('select[name=year],select[name=month],select[name=week],#btn').removeAttr('disabled');
                         tip.msgbox.err('此时间段的结算单已经生成，请到分红明细查看!');
                         return;
                     }

                    tip.alert('结算单生成成功!',function(){
                        location.href = "{php echo webUrl('fullownership/bonus/status0')}";
                    });

                    return;

                }
            });


        });

    }
    $(function () {
        $('select[name=year],select[name=month],select[name=week]').change(function () {
            loadDetail();
        });

    })
</script>
{/if}
{template '_footer'}
<!--OTEzNzAyMDIzNTAzMjQyOTE0-->